public class MutiplyBetter {
    public String multiply(String num1, String num2) {
        //方法二
        if (num1.charAt(0) == '0' || num2.charAt(0) == '0') {
            return new String("0");
        }
        int len1 = num1.length();
        int len2 = num2.length();
        int[] arr = new int[len1 + len2];
        for (int i = len1 - 1; i >= 0; i--) {
            StringBuilder sb = new StringBuilder();
            for (int j  = len1 - 1; j > i; j--) {
                sb.append(0);
            }
            int carry = 0;
            for (int j = len2 - 1; j >= 0; j--) {
                carry += ((num1.charAt(i) - '0') * (num2.charAt(j) - '0'));
                sb.append(carry % 10);
                carry /= 10;
            }
            if (carry > 0) {
                sb.append(carry);
                carry = 0;
            }
            int k = 0;
            for (; k < len1 + len2; k++) {
                carry += k < sb.length() ? sb.charAt(k) - '0' : 0;
                carry += arr[k];
                arr[k] = carry % 10;
                carry /= 10;
            }
            if (carry > 0) {
                arr[k] = carry;
            }
        }
        StringBuilder ret = new StringBuilder();
        for (int i = len1 + len2 - 1; i >= 0; i--) {
            if (arr[i] != 0 || ret.length() != 0) {  //删除前置0
                ret.append(arr[i]);
            }
        }
        return ret.toString();
    }
}
